<?php
/**
 * 公交的控制器
 * User: Fiend
 * Date: 13-10-13
 * Time: 下午6:51
 */

class Bus extends MY_Controller {
    function index(){
        $this->load->view('index');
    }

    /**
     * 搜索某一個線路
     */
    function search_by_linename(){
        $linename = html_escape($_POST['linename']);
        $this->load->model('bus_model','b');
        $data = $this->b->SearchByLinename($linename);
        $this->returnjson(array('ret'=>0,'msg'=>'success','data'=>$data));
    }

    /**
     * 查看某一個線路
     * @param int $lineid
     */
    function line_detail($lineid=0){
        $this->load->model('bus_model','b');
        $data = $this->b->GetLineStations($lineid);
        $this->returnjson(array('ret'=>0,'msg'=>'success','data'=>$data));
    }

    /**
     * 獲得總共線路的條數
     */
    function line_count(){
        $this->load->model('bus_model','b');
        $data = $this->b->GetLineCount();
        $this->returnjson(array('ret'=>0,'msg'=>'success','data'=>$data));
    }

    /**
     * 查询城市开放的所有线路的概要信息
     * @param int $citycode 城市代码
     */
    function get_opening_lines($citycode = 860515){
        //http://busapi.gpsoo.net/v1/bus/t_get_opening_lines
        $a = $this->request('http://busapi.gpsoo.net/v1/bus/t_get_opening_lines?citycode='.$citycode);
        $this->returnjson($a,false);
    }

    /**
     * 所有線路
     * @param int $p
     */
    function all_bus($p=0){
        $p = (int)$p;
        $this->load->model('bus_model','b');
        $data = $this->b->ListAllLine($p*20,20);
        $this->returnjson(array('ret'=>0,'msg'=>'success','data'=>$data));
    }

    /**
     * 查詢指定座標附近的車站
     * @param $lat
     * @param $lng
     */
    function near_station($lat,$lng){
        $lat = 0 + $lat;
        $lng = 0 + $lng;
        //http://busapi.gpsoo.net/v1/bus/mbcommonservice?method=get_station_refpos&lng=114.0449026&lat=22.5684942
        $data = $this->request('http://busapi.gpsoo.net/v1/bus/mbcommonservice?method=get_station_refpos&lng='.$lng.'&lat='.$lat);
        $this->returnjson($data);
    }

    /**
     * 查询城市开放的所有线路的详细信息
     * @param int $citycode
     */
    function get_openlines($citycode = 860515){
        //http://busapi.gpsoo.net/v1/bus/t_get_openlines
        $this->returnjson($this->request('http://busapi.gpsoo.net/v1/bus/t_get_openlines?citycode='.$citycode),false);
    }

    /**
     * 根据车站获得路线名
     * @param $station_name
     */
    function get_lines_by_station(){
        $station_name = html_escape($_POST['name']);
        $this->load->model('bus_model','b');
        $data = $this->b->GetLinesByStation($station_name);
        $this->returnjson(array('ret'=>0,'msg'=>'success','data'=>$data));
    }

    /**
     * 获得某条线路的具体信息，行进路线和车站，以及当前车站。
     * @param $subline
     * @param string $station
     * @param string $lng
     * @param string $lat
     */
    function get_line_info($subline,$station='',$lng='',$lat=''){
        //http://busapi.gpsoo.net/v1/bus/mbcommonservice?method=getlineinfo&sublineID=$subline&stationId=$station&lng=$lng&lat=$lat
        $this->returnjson($this->request("http://busapi.gpsoo.net/v1/bus/mbcommonservice?method=getlineinfo&sublineID=$subline&stationId=$station&lng=$lng&lat=$lat"),false);
    }

    /**
     * 附近的公交车
     * @param int $busid
     * @param int $stationid
     */
    function near_cars($busid=0,$stationid=0){
        $busid = (int)$busid;
        $stationid = (int)$stationid;
        $data = $this->request("http://busapi.gpsoo.net/v1/bus/mbcommonservice?method=getnearcarinfo&ids=&sublineid=$busid&stationId=$stationid");
        $this->returnjson($data,false);
    }

}